#include "operator.h"
#include <math.h>

//Armazena valores iniciais e define a prioridade do operador.
Operator::Operator(OpType ot, QString content):m_type(ot),m_content(content){
    switch (m_type) {
        case SUM:
        case SUB:{
            m_priority = LOW;
            break;
        }
        case MULT:
        case DIV: {
            m_priority = HIGH;
            break;
        }
        default:{ m_priority = HIGH; break; }
    }
}
Operator::~Operator(){}

//Converte os numeros necessarios para double, executa a operacao necessaria e reconverte para QString.
QString
Operator::exec(QString num){
   double first = m_content.toDouble();
   double second = num.toDouble();
   double retValue;
   QString retValueS;

    switch(m_type) {
        case SUM:{
            retValue = first+second;
            break;
        }
        case SUB:{
            retValue = first-second;
            break;
        }
        case DIV:{
            retValue = first/second;
            break;
        }
        case MULT:{
            retValue = first*second;
            break;
        }
        case POW:{
            retValue = pow(first,second);
            break;
        }
        case MOD:{
            retValue = fmod(first,second);
            break;
        }
        case YSQX:{
            retValue = pow(first, 1.0/second);
            break;
        }
        default:{
            retValue = first+second;
            break;
        }
    }
    retValueS.setNum(retValue);
    return retValueS;
}
